package com.coolweather.app.db;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.coolweather.app.modle.City;
import com.coolweather.app.modle.Country;
import com.coolweather.app.modle.Province;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 鏁版嵁搴撴搷浣滅被
 */
public class CoolWeatherDB {
	/**
	 * 鏁版嵁搴撳悕瀛�
	 */
	public static final String DB_NAME = "cool_weather";
	/**
	 * 鏁版嵁搴撶増鏈�
	 */
	public static final int VERSION = 1;

	private static CoolWeatherDB coolWeatherDB;

	private SQLiteDatabase db;

	// 灏嗘暟鎹簱绉佹湁鍖�
	private CoolWeatherDB(Context context) {
		CoolWeatherOpenHelper helper = new CoolWeatherOpenHelper(context, DB_NAME, null, VERSION);
		db = helper.getWritableDatabase();
	}
	
	public synchronized static CoolWeatherDB getInstance(Context context)
	{
		if (coolWeatherDB == null) {
			coolWeatherDB = new CoolWeatherDB(context);
		}
		return coolWeatherDB;
	}
	
	/**
	 * 灏哖rovince淇℃伅鍌ㄥ瓨鍒癙rovince鏁版嵁搴撲腑
	 */
	public void saveProvince(Province province)
	{
		ContentValues values = new ContentValues();
		values.put("province_name", province.getProvinceName());
		values.put("province_code", province.getProvinceCode());
		db.insert("Province", null, values);
	}
	
	
	public List<Province> getProvinceList()
	{
		List<Province> list = new ArrayList<Province>();
		Cursor cursor = db.query("Province", null, null, null, null, null, null);
		if (cursor.moveToFirst())
		{
			while (cursor.moveToNext()) {
				String provinceName = cursor.getString(cursor.getColumnIndex("province_name"));
				String provinceCode = cursor.getString(cursor.getColumnIndex("province_code"));
				Province province = new Province();
				province.setProvinceName(provinceName);
				province.setProvinceCode(provinceCode);
				list.add(province);
			}
		}
		return list;
	}
	
	/**
	 * 灏咰ity淇℃伅鍌ㄥ瓨鍒癈ity鏁版嵁搴撲腑
	 */
	public void saveCity(City city)
	{
		ContentValues values = new ContentValues();
		values.put("city_name", city.getCityName());
		values.put("city_code", city.getCityCode());
		values.put("province_id", city.getProvinceId());
		db.insert("City", null, values);
	}
	
	public List<City> getCityList(int provinceId)
	{
		List<City> list = new ArrayList<City>();
		Cursor cursor = db.query("City", null, "province_id = ?", new String[]{String.valueOf(provinceId)}, null, null, null);
		if (cursor.moveToFirst()) {
			while(cursor.moveToNext())
			{
				String city_Name = cursor.getString(cursor.getColumnIndex("city_name"));
				String city_Code = cursor.getString(cursor.getColumnIndex("city_code"));
				int province_Id = cursor.getInt(cursor.getColumnIndex("province_id"));
				City city = new City();
				city.setCityCode(city_Code);
				city.setCityName(city_Name);
				city.setProvinceId(province_Id);
				list.add(city);
			}
		}
		
		return list;
	}
	
	/**
	 * 灏咰ountry淇℃伅鍌ㄥ瓨鍒癈ountry鏁版嵁搴撲腑
	 */
	public void saveCountry(Country country)
	{
		ContentValues values = new ContentValues();
		values.put("country_name", country.getCountryName());
		values.put("country_code", country.getCountryCode());
		values.put("city_id", country.getCityId());
		db.insert("Country", null, values);
	}
	
	public List<Country> getCountryList(int cityId)
	{
		List<Country> list = new ArrayList<Country>();
		Cursor cursor = db.query("Country", null, "city_id = ?", new String[]{String.valueOf(cityId)}, null, null, null);
		if (cursor.moveToFirst()) {
			while(cursor.moveToNext())
			{
				String country_Name = cursor.getString(cursor.getColumnIndex("country_name"));
				String country_Code = cursor.getString(cursor.getColumnIndex("country_code"));
				int city_Id = cursor.getInt(cursor.getColumnIndex("city_id"));
				Country country = new Country();
				country.setCityId(cityId);
				country.setCountryCode(country_Code);
				country.setCountryName(country_Name);
				list.add(country);
			}
		}
		return list;
	}
	
	public void createTables(Context context) throws IOException, XmlPullParserException
	{
		XmlResourceParser xmlParser = context.getAssets().openXmlResourceParser("citys.xml");
		int eventType = xmlParser.getEventType();
		while(eventType != XmlPullParser.END_DOCUMENT)
		{
			String name = xmlParser.getName();
			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				break;
			case XmlPullParser.START_TAG:
				break;
			case XmlPullParser.END_DOCUMENT:
				break;
			default:
				break;
			}
		}
	}
}